home *** CD-ROM | disk | FTP | other *** search
/ Java Primer Plus / Java Primer Plus (Waite Group Proess)(1996).iso / java_Win / demo / ImageTest / ImageCanvas.class (.txt) < prev    next >
Encoding:
Java Class File  |  1995-10-12  |  4.4 KB  |  221 lines

  1. import java.applet.Applet;
  2. import java.awt.Canvas;
  3. import java.awt.Color;
  4. import java.awt.Component;
  5. import java.awt.Event;
  6. import java.awt.Graphics;
  7. import java.awt.Image;
  8. import java.awt.Rectangle;
  9. import java.awt.image.FilteredImageSource;
  10. import java.awt.image.ImageFilter;
  11. import java.awt.image.ImageObserver;
  12. import java.awt.image.ImageProducer;
  13.  
  14. class ImageCanvas extends Canvas implements ImageObserver {
  15.    double hmult;
  16.    int xadd;
  17.    int yadd;
  18.    int imgw = -1;
  19.    int imgh = -1;
  20.    int scalew = -1;
  21.    int scaleh = -1;
  22.    boolean focus = false;
  23.    boolean usefilter = false;
  24.    static final int numalphas = 8;
  25.    int alpha = 7;
  26.    Image[] imagevariants = new Image[16];
  27.    ImageFilter colorfilter;
  28.    Image origimage;
  29.    Image curimage;
  30.    Applet applet;
  31.    static final long updateRate = 100L;
  32.  
  33.    public ImageCanvas(Applet app, Image img, double mult) {
  34.       this.applet = app;
  35.       this.origimage = img;
  36.       this.imagevariants[7] = this.origimage;
  37.       this.hmult = mult;
  38.       this.pickImage();
  39.       ((Component)this).reshape(0, 0, 100, 100);
  40.    }
  41.  
  42.    public void gotFocus() {
  43.       this.focus = true;
  44.       ((Component)this).repaint();
  45.    }
  46.  
  47.    public void lostFocus() {
  48.       this.focus = false;
  49.       ((Component)this).repaint();
  50.    }
  51.  
  52.    public void paint(Graphics g) {
  53.       Rectangle r = ((Component)this).bounds();
  54.       if (this.focus) {
  55.          g.setColor(Color.red);
  56.       } else {
  57.          g.setColor(Color.darkGray);
  58.       }
  59.  
  60.       g.drawRect(0, 0, r.width - 1, r.height - 1);
  61.       g.drawLine(0, 0, r.width, r.height);
  62.       g.drawLine(r.width, 0, 0, r.height);
  63.       g.drawLine(0, r.height / 2, r.width, r.height / 2);
  64.       g.drawLine(r.width / 2, 0, r.width / 2, r.height);
  65.       if (this.imgw < 0) {
  66.          this.imgw = this.curimage.getWidth(this);
  67.          this.imgh = this.curimage.getHeight(this);
  68.          if (this.imgw < 0 || this.imgh < 0) {
  69.             return;
  70.          }
  71.       }
  72.  
  73.       if (this.scalew < 0) {
  74.          this.scalew = (int)((double)this.imgw * this.hmult);
  75.          this.scaleh = (int)((double)this.imgh * this.hmult);
  76.       }
  77.  
  78.       if (this.imgw == this.scalew && this.imgh == this.scaleh) {
  79.          g.drawImage(this.curimage, this.xadd, this.yadd, this);
  80.       } else {
  81.          g.drawImage(this.curimage, this.xadd, this.yadd, this.scalew, this.scaleh, this);
  82.       }
  83.    }
  84.  
  85.    public synchronized boolean imageUpdate(Image img, int infoflags, int x, int y, int w, int h) {
  86.       if (img != this.curimage) {
  87.          return false;
  88.       } else {
  89.          boolean ret = true;
  90.          boolean dopaint = false;
  91.          long updatetime = 0L;
  92.          if ((infoflags & 1) != 0) {
  93.             this.imgw = w;
  94.             dopaint = true;
  95.          }
  96.  
  97.          if ((infoflags & 2) != 0) {
  98.             this.imgh = h;
  99.             dopaint = true;
  100.          }
  101.  
  102.          if ((infoflags & 48) != 0) {
  103.             dopaint = true;
  104.             ret = false;
  105.          } else if ((infoflags & 8) != 0) {
  106.             dopaint = true;
  107.             updatetime = 100L;
  108.          }
  109.  
  110.          if ((infoflags & 64) != 0) {
  111.             ret = false;
  112.          }
  113.  
  114.          if (dopaint) {
  115.             ((Component)this).repaint(updatetime);
  116.          }
  117.  
  118.          return ret;
  119.       }
  120.    }
  121.  
  122.    public synchronized Image pickImage() {
  123.       int index = this.alpha;
  124.       if (this.usefilter) {
  125.          index += 8;
  126.       }
  127.  
  128.       Image choice = this.imagevariants[index];
  129.       if (choice == null) {
  130.          choice = this.imagevariants[this.alpha];
  131.          if (choice == null) {
  132.             int alphaval = this.alpha * 255 / 7;
  133.             ImageFilter imgf = new AlphaFilter(alphaval);
  134.             ImageProducer src = this.origimage.getSource();
  135.             ImageProducer var8 = new FilteredImageSource(src, imgf);
  136.             choice = this.applet.createImage(var8);
  137.             this.imagevariants[this.alpha] = choice;
  138.          }
  139.  
  140.          if (this.usefilter) {
  141.             if (this.colorfilter == null) {
  142.                this.colorfilter = new RedBlueSwapFilter();
  143.             }
  144.  
  145.             ImageProducer src = choice.getSource();
  146.             ImageProducer var7 = new FilteredImageSource(src, this.colorfilter);
  147.             choice = this.applet.createImage(var7);
  148.          }
  149.  
  150.          this.imagevariants[index] = choice;
  151.       }
  152.  
  153.       this.curimage = choice;
  154.       return choice;
  155.    }
  156.  
  157.    public synchronized boolean handleEvent(Event e) {
  158.       switch (e.id) {
  159.          case 401:
  160.          case 403:
  161.             switch (e.key) {
  162.                case 114:
  163.                case 7:
  164.                   this.xadd += 5;
  165.                   ((Component)this).repaint();
  166.                   return true;
  167.                case 6:
  168.                   this.xadd -= 5;
  169.                   ((Component)this).repaint();
  170.                   return true;
  171.                case 5:
  172.                   this.yadd += 5;
  173.                   ((Component)this).repaint();
  174.                   return true;
  175.                case 4:
  176.                   this.yadd -= 5;
  177.                   ((Component)this).repaint();
  178.                   return true;
  179.                case 3:
  180.                   if ((e.modifiers & 1) != 0) {
  181.                      if (--this.alpha < 0) {
  182.                         this.alpha = 0;
  183.                      }
  184.  
  185.                      this.pickImage();
  186.                   } else {
  187.                      this.hmult /= 1.2;
  188.                   }
  189.  
  190.                   this.scalew = this.scaleh = -1;
  191.                   ((Component)this).repaint();
  192.                   return true;
  193.                case 2:
  194.                   if ((e.modifiers & 1) != 0) {
  195.                      if (++this.alpha > 7) {
  196.                         this.alpha = 7;
  197.                      }
  198.  
  199.                      this.pickImage();
  200.                   } else {
  201.                      this.hmult *= 1.2;
  202.                   }
  203.  
  204.                   this.scalew = this.scaleh = -1;
  205.                   ((Component)this).repaint();
  206.                   return true;
  207.                case 0:
  208.                   this.usefilter = !this.usefilter;
  209.                   this.pickImage();
  210.                   ((Component)this).repaint();
  211.                   return true;
  212.                default:
  213.                   return false;
  214.             }
  215.          case 402:
  216.          default:
  217.             return false;
  218.       }
  219.    }
  220. }
  221.